package com.yjs.app.trade.transaction.util;

import java.io.Serializable;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;


/**
 * 结果（用于service层的返回值）
 * 
 * @since 1.0.0
 * @param <T>
 *            通常是业务对象
 */
public class Result implements Serializable {
	
	private static final long serialVersionUID = -740847433914461854L;
	
	public static JsonConfig jsonConfig = new JsonConfig();
	
	public static String desKey = "yjs_trade_app";
	
	public static long getSerialversionUID() {
		return serialVersionUID;
	}

	/**
	 * 是否成功
	 */
	private Boolean success = true;

	/**
	 * 数据（返回单条数据，一般用于新增、修改等操作的返回值）
	 */
	private Object data;
	
	/**
	 * 错误信息返回
	 */
	private String code;
	
	/**
	 * 错误信息返回
	 */
	private String message;
	
	private Result(Boolean success) {
		this.success = success;
	}

	private Result(Boolean success, Object data) {
		this.success = success;
		if (data instanceof Error) {
			//code = ((Error) data).getCode();
			//message = ((Error) data).getRemark();
		} else {
			this.data = data;
		}
	}
	
	private Result(String code,String message) {
		this.success = false;
		this.code = code;
		this.message = message;
	}
	
	public Object getData() {
		return data;
	}

	public static <T> Result success() {
		return new Result(true);
	}
	
	public static <T> Result success(Object data) {
		return new Result(true ,data);
	}
	
	public static <T> Result fail(Object data) {
		return new Result(false, data);
	}
	
	public static <T> Result fail(String code, String name) {
		return new Result(code, name);
	}
	
	public static <T> String csuccess() {
		return DESUtils.getEncryptStr(JSONObject.fromObject(new Result(true), jsonConfig).toString(), desKey);
	}
	
	public static <T> String csuccess(Object data) {
		return DESUtils.getEncryptStr(JSONObject.fromObject(new Result(true,data), jsonConfig).toString(), desKey);
	}
	
	public static <T> String cfail(Object data) {
		return DESUtils.getEncryptStr(JSONObject.fromObject(new Result(false, data), jsonConfig).toString(), desKey);
	}
	
	public static <T> String cfail(String code, String name) {
		return DESUtils.getEncryptStr(JSONObject.fromObject(new Result(code, name), jsonConfig).toString(), desKey);
	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public void setData(Object data) {
		this.data = data;
	}

}
